<?php
if ( !defined('IN_ADMINCP') || !defined('IN_SEC') ){
	exit('Access Denied.');
}

$id = abs(intval($_G['gp_id']));

if( $id && !$team = fetch('team', $id) ){
	$admincp->cpmsg('项目不存在');
}
if( $team ){
//	$team['members'] = parse_array(limitquery('team_member', array('teamid' => $team['id'])), 'adminid');
}

if( $team && $_G['gp_detail'] == 'yes' && !is_post() ){
	$missions = limitquery('team_mission', array(
		'teamid' => $team['id'],
	));
	$tasks = limitquery('team_task', array(
		'teamid' => $team['id'],
	));
	$datas = array();
	foreach( $tasks as $k => $v ){
		$datas['missions'][$v['missionid']]['teams'][] = $v;
	}
	$tmpname = '_detail';
}

$customers = limitquery('customer');

$departments = parse_array(limitquery('admincp_department'));
$admins = array();
foreach( $admincp->admins as $k => $v ){
	$admins[$v['id']] = array(
		'id' => $v['id'],
		'name' => $departments[$v['departmentid']]['name'] . ' - ' . heivrid($v)
	);
}
$members = array();
foreach( $admins as $k => $v ){
	if( $v['departmentid'] ){
		$members[$v['departmentid']][$v['id']] = $v;
	}
}
if( is_post() ){
	if( !confirm_password($admincp->admin) ){
		$admincp->cpmsg('密码不正确！授权操作失败！');
	}
	$name = $_G['gp_name'] ? htmlspecialchars($_G['gp_name']) : $admincp->cpmsg('请填写项目名称');
	$customerid = abs(intval($_G['gp_customerid']));
	$customerid = $customerid ? $customerid : $admincp->cpmsg('请选择客户');

	$members = $_G['gp_member'] ? $_G['gp_member'] : array();

	$teamid = $team['id'];
	$update = array(
		'name' => $name,
		'customerid' => $customerid,
		'adminid' => $admincp->adminid,
		'creatorid' => $admincp->adminid,
	);
	if( $team ){
		update('team', $team['id'], $update);
		$admincp->log('team', $temid, '编辑项目：' . $name, $update);
	}else{
		$update['create_time'] = SYS_TIME;
		$teamid = DB::Insert('team', $update);
		$admincp->log('team', $temid, '添加项目：' . $name, $update);
	}

	foreach( $members as $k => $v ){
		if( !$team['members'][$k] ){
			DB::Insert('team_member', array(
				'teamid' => $teamid,
				'adminid' => $k,
				'create_time' => SYS_TIME
			));
		}
	}

	$admincp->success();
}

?>